package example;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Properties;

//查询所有
//1.sql:update tb_brand set brand_name =?,company_name=?,ordered=?,description=?,status=? where id = ?
//2.参数：需要
//3.结果：List<Brand>
public class BrandUpdate {
    public static void main(String[] args) throws Exception {
        //模拟页面提交的参数
        String brandName = "香飘飘";
        String companyName = "香飘飘";
        int ordered = 1;
        String description = "绕地球三圈";
        int status = 1;
        int id = 4;
        //1.获取connection
        Properties properties = new Properties();
        properties.load(new FileInputStream("jdbc-demo/src/main/resources/druid.properties"));
        //4.获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        //5.获取对应的数据库连接
        Connection conn = dataSource.getConnection();
        //2.定义sql
        String sql = "update tb_brand set brand_name =?,company_name=?,ordered=?,description=?,status=? where id = ?";
        //3.获取pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
        //4.设置参数
        pstmt.setString(1,brandName);
        pstmt.setString(2,companyName);
        pstmt.setInt(3,ordered);
        pstmt.setString(4,description);
        pstmt.setInt(5,status);
        pstmt.setInt(6,id);
        //5.执行sql
        int count = pstmt.executeUpdate();//影响的行数
        //6.处理结果
        System.out.println(count>0);

        //7.释放资源
        pstmt.close();
        conn.close();
    }
}
